package com.tencent.sr.iris.basic.saas.service.export;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Maps;
import com.tencent.sr.iris.baisc.saas.export.sdk.TaskDependency;
import com.tencent.sr.iris.basic.saas.common.exception.BizErrorCode;
import com.tencent.sr.iris.basic.saas.common.utils.AssertUtils;
import com.tencent.sr.iris.basic.saas.common.utils.BizUtil;
import com.tencent.sr.iris.basic.saas.interfaces.request.activity.ActivityTaskPageRequest;
import com.tencent.sr.iris.basic.saas.interfaces.response.export.ExportBusinessTypeResponse;
import com.tencent.sr.iris.basic.saas.interfaces.response.export.ExportResponse;
import com.tencent.sr.iris.basic.saas.service.util.ValidateUtils;
import com.tencent.sr.rmall.material.interfaces.enums.ETaskBusinessType;
import com.tencent.sr.rmall.material.interfaces.enums.ETaskStatus;
import com.tencent.sr.rmall.material.interfaces.enums.ETaskType;
import com.tencent.sr.rmall.material.interfaces.request.AddTaskRecordRequest;
import com.tencent.sr.rmall.springbootstarter.authtoken.localbean.AppUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ExportService {

    @Resource
    private AppUser appUser;

    @Resource
    private TaskDependency taskDependency;

    private static final Map<String, TypeReference> EXPORT_REQUEST_CLASS_MAP = Maps.newHashMap();

    static {
        EXPORT_REQUEST_CLASS_MAP.put(ETaskBusinessType.IRIS_ACTIVITY_TASK_DETAIL.name(), new TypeReference<ActivityTaskPageRequest>(){});
    }

    public ExportResponse commonRequest(String exportType, String reqJsonStr) {
        TypeReference typeReference = EXPORT_REQUEST_CLASS_MAP.get(exportType);
        Object req = JSON.parseObject(reqJsonStr, typeReference);
        ValidateUtils.validate(req);

        ETaskBusinessType type = Arrays.stream(ETaskBusinessType.values())
                .filter(i -> Objects.equals(i.name(), exportType)).findFirst().orElse(null);
        AssertUtils.isNotNull(type, BizErrorCode.PARAM_CHECK_ERROR);

        return getExportResponse(type, reqJsonStr);
    }

    public List<ExportBusinessTypeResponse> businessTypeList() {
        return Arrays.stream(ETaskBusinessType.values()).map(i -> new ExportBusinessTypeResponse()
                .setBusinessType(String.valueOf(i.getStatus()))
                .setDesc(i.getDesc())).collect(Collectors.toList());
    }

    public <T> ExportResponse commonRequest(ETaskBusinessType eTaskBusinessType, T t) {
        String jsonString = JSON.toJSONString(t);
        return getExportResponse(eTaskBusinessType, jsonString);
    }

    /**
     * 导出
     * @param eTaskBusinessType
     * @param jsonString
     * @return
     */
    public ExportResponse getExportResponse(ETaskBusinessType eTaskBusinessType, String jsonString) {
        Long rootSaasId = BizUtil.getRootSaasId(appUser);
        String uid = BizUtil.getUid(appUser);
        AddTaskRecordRequest addTaskRecordRequest = new AddTaskRecordRequest();
        addTaskRecordRequest.setSaasId(rootSaasId);
        addTaskRecordRequest.setOperatorId(uid);
        addTaskRecordRequest.setTaskStatus(ETaskStatus.WAIT.getStatus());
        addTaskRecordRequest.setTaskType(ETaskType.EXPORT.getType());
        addTaskRecordRequest.setRequestParam(jsonString);
        log.info(eTaskBusinessType.getDesc() + "请求参数:{}", addTaskRecordRequest.getRequestParam());
        addTaskRecordRequest.setBusinessType(eTaskBusinessType.getStatus());
        addTaskRecordRequest.setReportFormName(genName(eTaskBusinessType));
        Long taskId = taskDependency.addTask(addTaskRecordRequest);
        ExportResponse exportTaskResponse = new ExportResponse();
        exportTaskResponse.setFileName(addTaskRecordRequest.getReportFormName());
        exportTaskResponse.setTaskId(taskId);
        log.info("export response:{}", JSON.toJSONString(exportTaskResponse));
        return exportTaskResponse;
    }


    private String genName(ETaskBusinessType t) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmm");
        String formatStr = formatter.format(new Date());
        return t.getDesc() + "-" + formatStr + ".xlsx";
    }

}
